﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace TY.Utilities
{
    public class FileOpera
    {
        /// 将文件大小(字节)转换为最适合的显示方式
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static string ConvertFileSize(int size)
        {
            string result = "0KB";
            int filelength = size.ToString().Length;
            if (filelength < 4)
                result = size + "byte";
            else if (filelength < 7)
                result = Math.Round(Convert.ToDouble(size / 1024d), 2) + "KB";
            else if (filelength < 10)
                result = Math.Round(Convert.ToDouble(size / 1024d / 1024), 2) + "MB";
            else if (filelength < 13)
                result = Math.Round(Convert.ToDouble(size / 1024d / 1024 / 1024), 2) + "GB";
            else
                result = Math.Round(Convert.ToDouble(size / 1024d / 1024 / 1024 / 1024), 2) + "TB";
            return result;
        }
        //检查路径是否存在，不存在则创建
        public static void createPath(string _path)
        {
            FileInfo fi = new FileInfo(_path);
            if (!Directory.Exists(_path))//如果路径不存在
            {
                Directory.CreateDirectory(_path); ;//创建路径
            }

        }
        //移动文件
        public static void moveFile(FileInfo file, string filepath)
        {
            if (file.Exists)
            {
                file.MoveTo(filepath);

            }

        }
        //移动文件
        public static void copyFile(FileInfo file, string filepath)
        {
            if (file.Exists)
            {
                file.CopyTo(filepath, true);

            }

        }
        //判断文件是否存在
        public static bool IsExists(string filepath)
        {
            return File.Exists(filepath);
        }
        //删除文件
        public static void deleteFile(string filepath)
        {
            if (File.Exists(filepath))
            {

                File.Delete(filepath);
            }
        }
        //删除文件夹和其中的所有文件
        public static void deleteDirectory(string directorypath)
        {
            if (Directory.Exists(directorypath))
            {
                DirectoryInfo di = new DirectoryInfo(directorypath);
                di.Delete(true);
            }
        }

        public static FileInfo GetFileInfo(string filepath)
        {
            return new FileInfo(filepath);

        }
        public static string GetTrueFileName(Stream fs)
        {
            System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
            string bx = " ";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                bx = buffer.ToString();
                buffer = r.ReadByte();
                bx += buffer.ToString();
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }
            r.Close();
            fs.Close();

            return bx;
        }
        public enum ImgExtension:int
        {
            JPG = 255216,
            GIF = 7173,
            BMP = 6677,
            PNG = 13780
            //COM = 7790,
            //EXE = 7790,
            //DLL = 7790,
            //RAR = 8297,
            //ZIP = 8075,
            //XML = 6063,
            //HTML = 6033,
            //ASPX = 239187,
            //CS = 117115,
            //JS = 119105,
            //TXT = 210187,
            //SQL = 255254,
            //BAT = 64101,
            //BTSEED = 10056,
            //RDP = 255254,
            //PSD = 5666,
            //PDF = 3780,
            //CHM = 7384,
            //LOG = 70105,
            //REG = 8269,
            //HLP = 6395,
            //DOC = 208207,
            //XLS = 208207,
            //DOCX = 208207,
            //XLSX = 208207,
        }
        //foreach (int  myCode in Enum.GetValues(typeof(eErrorDetailCode)))
        //     {
        //         string strName =Enum.GetName(typeof(eErrorDetailCode), myCode);//获取名称
        //         string strVaule = myCode.ToString();//获取值
        //         ListItem myLi = new ListItem(strName,strVaule);
        //         ddlType.Items.Add(myLi);//添加到DropDownList控件
        //     }
    }
}
